<template>
    <div class="content">
        <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" label-width="auto" class="demo-ruleForm" size="large" status-icon >
            <el-form-item label="选择岗位" prop="role_id">
                <el-select v-model="ruleForm.role_id" clearable >
                    <el-option label="请选择" :value="0" />
                    <el-option v-for="item in tableData" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="选择人员" prop="user_id">
                <el-select v-model="ruleForm.user_id" clearable  @change="onUser">
                    <el-option label="请选择" :value="0" />
                    <el-option v-for="item in userData" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="选择证书" prop="cert_id">
                <el-select v-model="ruleForm.cert_id" clearable >
                    <el-option label="请选择" :value="0" />
                    <el-option v-for="item in certData" :key="item.id" :label="item.title" :value="item.id" />
                </el-select>
            </el-form-item>
        </el-form>
        <div class="text-center">
            <el-button type="primary" @click="onInsert">确定</el-button>
        </div>
    </div>

</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import http from '../../../../util/request';

const ruleForm = ref<any>({role_id:0,user_id:0,cert_id:0,id:0})

const tableData = ref<any[]>([])
const certData = ref<any[]>([])
const userData = ref<any[]>([])

const userId = ref<number>(0)
const onGetData = () => {
    http.get('/roleUser/'+userId.value,{}).then((response:any)=>{
        if(!response.data.data){
            return;
        }
        ruleForm.value = response.data.data
        onGetCert()
    })
}

const onGetRole = () => {
    http.post('/role',{limit:99}).then((response:any)=>{
        tableData.value = response.data.data
    })
}

const onGetUser = () => {
    http.post('/user/all',{}).then((response:any)=>{
        userData.value = response.data.data
    })
}

const onGetCert = () => {
    http.post('/user/certs',{id:ruleForm.value.user_id}).then((response:any)=>{
        certData.value = response.data.data
    })
}

const onUser = (id:number) => {
    ruleForm.value.user_id = id
    onGetCert()
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    userId.value = props.data
    onGetData()
    onGetRole()
    onGetUser()
},{ immediate: true })

const onInsert = () => {
    if(!ruleForm.value.role_id){
        ElMessage.error('请选择岗位');
        return
    }

    if(!ruleForm.value.user_id){
        ElMessage.error('请选择人员');
        return
    }
    let request:any = null
    if(ruleForm.value.id){
        request = http.patch('/role/user',ruleForm.value)
    }else{
        request = http.put('/role/user',ruleForm.value)
    }

    request.then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        saveBind();
    })
}

const saveBind = () => {
    emit('saveBind')
}

const emit = defineEmits(['saveBind'])

</script>

<style scoped>

</style>